package com.atguigu.ssyx.acl.Utils;

import com.atguigu.ssyx.model.acl.Permission;

import java.util.ArrayList;
import java.util.List;

public class PermissionHelper {


    public static List<Permission> buildPermission(List<Permission> allLists) {
        List<Permission> trees = new ArrayList<>();
        for (Permission lists : allLists) {
            if (lists.getPid() == 0) {
                lists.setLevel(1);
//                找到顶级节点.
                trees.add(lists);
                buildPermissionTree(lists, allLists);
            }
        }
        return trees;
    }


    public static Permission buildPermissionTree(Permission permission, List<Permission> allLists) {
        permission.setChildren(new ArrayList<Permission>());
        //递归子节点.
        for (Permission list : allLists) {
            if (list.getPid() == permission.getId()) {

                int level = permission.getLevel() + 1;
                list.setLevel(level);

                permission.getChildren().add(list);
                buildPermissionTree(list, allLists);
            }
        }
        return permission;
    }
}
